package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class deletepingl
 */
@WebServlet("/deletepingl")
public class deletepingl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deletepingl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: deletepingl").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String deleteppldid = request.getParameter("deleteppldid");
		String bkidsdelete = request.getParameter("bkidsdelete");
		System.out.println("deleteppldid:" + deleteppldid);
		System.out.println("bkidsdelete:" + bkidsdelete);
		try {
		    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
		    e.printStackTrace();
	    }
	    Connection cont13 = null;
	    PreparedStatement prestn13 = null;
	    try {
	    	cont13 = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
	    	Statement stmt = cont13.createStatement();
		    String sctpye = "delete from review where id = ?";
		    prestn13 = cont13.prepareStatement(sctpye);
		    prestn13.setString(1, deleteppldid);
		    if (!deleteppldid.equals("")) {
		    	prestn13.executeUpdate();
			}
			String sqlpll = "select count(id) total from review where bei_id = " + bkidsdelete;
			ResultSet rspll = stmt.executeQuery(sqlpll);
			int setnumpll = 0;
			while (rspll.next()) {
				int getnumpll = rspll.getInt(1);
				setnumpll = getnumpll;
			}
			System.out.println("blog_pingl_num:" + setnumpll);
			PreparedStatement psthcppl;
			String sqlhc = "update blog set comment_count = " + setnumpll + " where id = ?";
			psthcppl = cont13.prepareStatement(sqlhc);
			psthcppl.setString(1, bkidsdelete);
			psthcppl.executeUpdate();
			psthcppl.close();
			rspll.close();
		    prestn13.close();
		    cont13.close();
            response.sendRedirect("/admin_bloggl.jsp");
	    } catch (SQLException e) {
		    e.printStackTrace();
	    }
	}

}
